﻿using System;
using System.Collections.Generic;
using System.Web.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;

public partial class UserInformationforADMIN : System.Web.UI.Page
{
    SqlConnection conn;
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds;
    string strconn;
    protected void Page_Load(object sender, EventArgs e)
    {

        strconn = WebConfigurationManager.ConnectionStrings["photoconnection"].ToString();
        conn = new SqlConnection(strconn);
        ds = new DataSet();
        if (!IsPostBack)
            bindintogridview();
    }

    protected void bindintogridview()
    {
        try
        {
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            cmd = new SqlCommand("pdselect", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            dgv.DataSource = null;
            dgv.DataSource = ds.Tables[0];
            dgv.DataBind();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }
    public void delete(string id)
    {
        try
        {
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            cmd = new SqlCommand("pddelete", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@id", id);
            cmd.ExecuteNonQuery();

        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }
    protected void dgv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = dgv.Rows[e.RowIndex].Cells[0].Text;
        delete(id);
        bindintogridview();
    }
    protected void dgv_RowEditing(object sender, GridViewEditEventArgs e)
    {
        dgv.EditIndex = e.NewEditIndex;
        bindintogridview();
    }
    protected void dgv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        dgv.EditIndex = -1;
        bindintogridview();
    }

    public void update(int id, string user, string pass, string name, int age, string mail,  string country,string img)
    {
        try
        {
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            cmd = new SqlCommand("pdupdate", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@id", id);
            cmd.Parameters.AddWithValue("@username", user);
            cmd.Parameters.AddWithValue("@password", pass);
            cmd.Parameters.AddWithValue("@Name", name);
            cmd.Parameters.AddWithValue("@Age", age);
            cmd.Parameters.AddWithValue("@Email", mail);
           
            cmd.Parameters.AddWithValue("@Country", country);
            cmd.Parameters.AddWithValue("@ImageUpload", img);
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }
    protected void dgv_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = Convert.ToInt32(((TextBox)dgv.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
        string user = ((TextBox)dgv.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        string pass = ((TextBox)dgv.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        string name = ((TextBox)dgv.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
        int age = Convert.ToInt32(((TextBox)dgv.Rows[e.RowIndex].Cells[4].Controls[0]).Text);
        string mail = ((TextBox)dgv.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
        
        string country = ((TextBox)dgv.Rows[e.RowIndex].Cells[6].Controls[0]).Text;
        string img = ((TextBox)dgv.Rows[e.RowIndex].Cells[7].Controls[0]).Text;
        update(id, user, pass, name, age, mail, country,img);
        dgv.EditIndex = -1;
        bindintogridview();
    }
}